#define _CRT_SECURE_NO_WARNINGS 1
#include "seqlist1.h"

void SLprint(SL* pf)
{
	int i = 0;
	for (i = 0; i < pf->size; i++)
	{
		printf("%d ", pf->arr[i]);
	}
	printf("\n");
}

void SLinit(SL* pf)
{
	pf->arr = NULL;
	pf->memory = pf->size = 0;
}

void SLdate(SL* pf)
{
	if (pf->arr)
	{
		free(pf);
	}
	pf->arr = NULL;
	pf->memory = pf->size = 0;
}

void SLmemory(SL* pf)
{
	if (pf->memory == pf->size)
	{
		int newmemory = pf->memory == 0 ? 4 : 2 * pf->memory;
		SLtype* ret = (SLtype*)realloc(pf->arr, newmemory * sizeof(SLtype));
		if (ret == NULL)
		{
			perror("realloc:ret");
			exit(-1);
		}
		pf->arr = ret;
		pf->memory = newmemory;
	}
}

void SLpushback(SL* pf, SLtype x)
{
	SLmemory(pf);
	pf->arr[pf->size] = x;
	pf->size++;
}

void SLpushfront(SL* pf, SLtype x)
{
	SLmemory(pf);
	int i = 0;
	for (i = pf->size; i >0 ; i--)
	{
		pf->arr[i] = pf->arr[i - 1];
	}
	pf->arr[0] = x;
	pf->size++;
}

void SLbackdate(SL* pf)
{
	assert(pf);
	assert(pf->arr);
	pf->size--;
}

void SLfrontdate(SL* pf)
{
	assert(pf);
	assert(pf->arr);
	int i = 0;
	for (i = 0; i < pf->size-1; i++)
	{
		pf->arr[i] = pf->arr[i + 1];
	}
	pf->size--;
}

int  findSL(SL* pf, SLtype x)
{
	int i = 0;
	for (i = 0; i < pf->size; i++)
	{
		if (pf->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SLfind(SL* pf, SLtype x)
{
	assert(pf);
	assert(pf->arr);
	int find = findSL(pf, x);
	if (find > 0)
	{
		printf("this find is succes->:%d", find);
	}
	else
	{
		printf("this find is faild");
	}
}

void SLfinddate(SL* pf, SLtype x)
{
	int find = findSL(pf, x);
	if (find > 0)
	{
		int i = 0;
		for (i = find; i < pf->size; i++)
		{
			pf->arr[i] = pf->arr[i + 1];
		}
		pf->size--;
	}
}

void SLfindputpush(SL* pf, int put,SLtype x)
{
	if (put >= 0 && put <= pf->size)
	{
		SLmemory(pf);
		int i = 0;
		for (i = pf->size - 1; i > put-1; i--)
		{
			pf->arr[i+1] = pf->arr[i];
		}
		pf->arr[put] = x;
		pf->size++;
	}
	else
	{
		printf("put is NULL\n");
		return 1;
	}
}

